package com.gcsoftware.web.mapper;

import com.gcsoftware.web.beans.T_GW_APPS;
import com.gcsoftware.web.beans.T_GW_APP_IPS;
import com.gcsoftware.web.beans.T_GW_APP_PWDS;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by qs on 2018/3/23.
 */
@Mapper
public interface T_GW_APPSMAPPER {

    @Select("<script>" +
            "SELECT " +
            "apps.C_APP_ID ,roles.c_ROLE_ID,roles.c_ROLE_NAME,pwd.c_PASSWORD,C_APP_NAME ,C_APP_TYPE,tapp.c_DICT_ITEM_NAME c_APP_TYPE_NAME ,pwd.c_PASSWORD,apps.C_COMPANY_ID,com.c_COMPANY_NAME,C_APP_STATUS,dict.c_DICT_ITEM_NAME c_APP_STATUS_NAME ,C_APP_REMARK,to_char(apps.D_CREATE_TIME,'yyyy-MM-dd hh:mi') c_CREATE_TIME " +
            "FROM " +
            "T_GW_APPS apps " +
            "left join t_GW_APP_PWDS pwd on pwd.C_APP_ID = apps.c_APP_ID and pwd.c_PWD_STATUS='PWD_STATUS_NORMAL' and apps.C_APP_STATUS ='APP_STATUS_PUBLISH' " +
            "LEFT JOIN T_GW_ROLES roles ON roles.c_ROLE_ID = apps.c_ROLE_ID, " +
            "T_GW_COMPANY com,T_COM_SYS_DICTITEM  dict,T_COM_SYS_DICTITEM tapp " +
            "where " +
            "com.c_COMPANY_ID = apps.c_COMPANY_ID " +
            "and tapp.c_DICT_ITEM_VALUE = apps.c_APP_TYPE " +
            "and tapp.c_DICT_GROUP_VALUE='GRP_APP_TYPES' " +
            "and dict.C_DICT_ITEM_VALUE =c_APP_STATUS " +
            "and dict.C_DICT_GROUP_VALUE ='GRP_APP_STATUS'" +
            "<when test='null != c_APP_NAME'> AND c_APP_NAME like concat(concat('%',#{c_APP_NAME}),'%') </when>" +
            "<when test='null != c_APP_STATUS'> AND c_APP_STATUS =#{c_APP_STATUS}</when>" +
            "<when test='null != c_APP_TYPE'> AND c_APP_TYPE =#{c_APP_TYPE}</when>" +
            "<when test='null == c_COMPANY_ID'> AND apps.c_APP_STATUS != 'APP_STATUS_INIT'</when>" +
            "<when test='null != c_COMPANY_ID'> AND apps.c_COMPANY_ID =#{c_COMPANY_ID}</when>" +
            "</script>")
    public List<T_GW_APPS> queryForList(
            @Param(value = "c_APP_NAME")String c_APP_NAME,
            @Param(value = "c_APP_STATUS") String c_APP_STATUS,
            @Param(value = "c_APP_TYPE") String c_APP_TYPE,
            @Param(value = "c_COMPANY_ID") String c_COMPANY_ID);

    @Select("<script>SELECT app.C_APP_ID,C_APP_NAME,pwd.c_PASSWORD,c_APP_TYPE,tp.c_dict_item_name C_APP_TYPE_NAME,app.C_COMPANY_ID,com.c_company_name,C_APP_STATUS,C_APP_REMARK,app.c_ROLE_ID,role.c_ROLE_NAME FROM T_GW_APPS app left join t_GW_APP_PWDS pwd on pwd.c_APP_ID = app.c_APP_ID and c_PWD_STATUS = 'PWD_STATUS_NORMAL' LEFT JOIN T_GW_ROLES role ON role.C_ROLE_ID = app.c_ROLE_ID,t_com_sys_dictitem tp,t_gw_company com WHERE com.c_company_id = app.c_company_id and tp.c_dict_item_value = app.c_app_type <when test='null != c_APP_ID'> AND app.c_APP_ID = #{c_APP_ID}</when><when test='null != c_APP_NAME'> AND c_APP_NAME = #{c_APP_NAME} AND app.c_COMPANY_ID=#{c_COMPANY_ID}</when></script>")
    public T_GW_APPS get(@Param(value = "c_APP_ID") String c_APP_ID , @Param(value = "c_APP_NAME") String c_APP_NAME, @Param(value = "c_COMPANY_ID") String c_COMPANY_ID);

    @Insert("Insert INTO T_GW_APPS (c_APP_ID,c_APP_NAME,c_APP_TYPE,c_COMPANY_ID,c_APP_REMARK)VALUES(#{c_APP_ID},#{c_APP_NAME},#{c_APP_TYPE},#{c_COMPANY_ID},#{c_APP_REMARK})")
    public int insertApps(T_GW_APPS apps);

    @Update("UPDATE T_GW_APPS SET D_MODIFY_TIME = CURRENT_TIMESTAMP(),c_APP_NAME = #{c_APP_NAME},C_APP_TYPE=#{c_APP_TYPE},C_APP_REMARK=#{c_APP_REMARK} WHERE c_APP_ID = #{c_APP_ID}")
    public int updateApps(T_GW_APPS apps);

    @Delete("DELETE T_GW_APPS WHERE c_APP_ID=#{c_APP_ID}")
    public int deleteApps(String c_APP_ID);

    @Update("UPDATE T_GW_APPS SET D_MODIFY_TIME = CURRENT_TIMESTAMP(),c_APP_STATUS=#{c_APP_STATUS} WHERE c_APP_ID = #{c_APP_ID}")
    public int updateAppStatus(@Param(value = "c_APP_ID") String c_APP_ID,@Param(value = "c_APP_STATUS") String c_APP_STATUS);

    @Update("UPDATE T_GW_APPS SET D_MODIFY_TIME = CURRENT_TIMESTAMP(),c_ROLE_ID=#{c_ROLE_ID} WHERE c_APP_ID = #{c_APP_ID}")
    public int updateAppRole(@Param(value = "c_APP_ID") String c_APP_ID,@Param(value = "c_ROLE_ID") String c_ROLE_ID);


    @Insert("INSERT INTO T_GW_APP_IPS(C_APP_IP_ID ,c_APP_ID,C_APP_IP ,C_APPIP_REMARK)VALUES(#{c_APP_IP_ID},#{c_APP_ID},#{c_APP_IP},#{c_APPIP_REMARK})")
    public int addAppIps(T_GW_APP_IPS ips);


    @Delete("DELETE T_GW_APP_IPS WHERE C_APP_IP_ID = #{c_APP_IP_ID}")
    public int deleteAppIp(String c_APP_IP_ID);

    @Delete("DELETE T_GW_APP_IPS WHERE c_APP_ID = #{c_APP_ID}")
    public int batchDeleteIp(String c_APP_ID);

    @Select("SELECT max(C_APP_IP_ID) FROM T_GW_APP_IPS")
    public String getMaxAppIPId();

    @Select("SELECT max(C_APP_PWD_ID) FROM T_GW_APP_PWDS")
    public String getMaxPwdId();

    @Select("SELECT C_APP_IP_ID,c_APP_ID,c_APP_IP,C_APPIP_REMARK FROM T_GW_APP_IPS WHERE C_APP_ID = #{c_APP_ID}")
    public List<T_GW_APP_IPS> queryIpsList(@Param(value = "c_APP_ID") String c_APP_ID);

    @Select("SELECT c_APP_IP FROM T_GW_APP_IPS WHERE C_APP_ID = #{c_APP_ID}")
    public List<String> queryIpstrList(@Param(value = "c_APP_ID") String c_APP_ID);

    @Update("UPDATE T_GW_APP_PWDS SET D_MODIFY_TIME = CURRENT_TIMESTAMP() , C_PWD_STATUS = 'PWD_STATUS_UNABLE' WHERE c_APP_ID = #{c_APP_ID}")
    public int updateAppPwds(@Param(value = "c_APP_ID") String c_APP_ID);

    @Insert("INSERT INTO T_GW_APP_PWDS(c_APP_ID,c_PASSWORD,c_PWD_STATUS) VALUES (#{c_APP_ID},#{c_PASSWORD},#{c_PWD_STATUS})")
    public int insertAppPwd(T_GW_APP_PWDS pwd);
}
